<template>
	<view>
		<view style="
	     padding: 22rpx;
         position: fixed;
         width: 95%;
         background: rgb(255, 255, 255);
         margin-bottom: 22px;
		 border-bottom: #ebebeb61 solid;">
			<u-search shape="round" placeholder="请输入科室名称"  v-model="params.costDeptNm" @custom="butSearch" search="butSearch"></u-search>
		</view>
		<view style="height: 100rpx;"></view>
		<view class="costdept" v-for="(item,index) in departmentList" @click="costSelect(item.costDeptNm,item.costDeptNo)">
			<view class="costdeptChilder">
				<view class="">科室名称</view>
				<view class="">{{item.costDeptNm}}</view>
			</view>
			<view class="costdeptChilder">
				<view class="">科室编码</view>
				<view class="">{{item.costDeptNo}}</view>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				params: {
					pageindex: 1,
					pagesize: 10,
					costDeptNo: "",
					costDeptNm: ""
				},
				departmentList:[],
				num:0,
				tripIndex:0
			}
		},
		watch:{
			'params.costDeptNm'(val){
                   this.butSearch();
			}
		},
		onLoad(e) {
			this.search();
			if(e.trip){
				this.tripIndex = e.trip;
			}
		},
		methods: {
			butSearch(){
				this.params.pageindex =1;
				this.departmentList=[];
				this.search();
			},
			search() {
				let _this = this;
				clearTimeout(_this.timer)  //清除延时器
				_this.timer = setTimeout(() => { //重新开启新的延时器
					_this.$lyRequest.get('/gateway/basedata/mp/dbsap/basedata/costDept/queryList.do', _this.params).then(data => {
						if (data.data.result == "1") {
							if (data.data.rows.length > 0) {
								_this.departmentList = [..._this.departmentList,...data.data.rows];
							}
						}
					})
				}, 500)
			},
			costSelect(costDeptNm,costDeptNo){
				let pages = getCurrentPages();
				let prePage = pages[pages.length - 2];
					prePage.$vm.travelAppList.busiTripdEntityList[this.tripIndex].subdeptId=costDeptNo;
					prePage.$vm.travelAppList.busiTripdEntityList[this.tripIndex].subdeptNm=costDeptNm;
					let _this = this;
					_this.$lyRequest.get('/gateway/budget/mp/dbsap/basedata/budget/queryList.do', 
					{budgetType: '1',
					 subdeptId: 'AD',
					 budgetYear: 2022}).then(data => {
						if (data.data.result == "1") {
							if (data.data.rows) { 
								prePage.$vm.travelAppList.busiTripdEntityList[_this.tripIndex].budgetNo = data.data.rows[0].budgetNo;
								prePage.$vm.travelAppList.busiTripdEntityList[_this.tripIndex].budgetNm = data.data.rows[0].budgetNm;
							}
						}
					});
				setTimeout(function(){
					uni.navigateBack();
				},500)
			}
		},
		   // 下拉到底部后加载新数据
		    onReachBottom() {
		      //判断下一页是否存在数据，不存在将显示暂无数据等提示语
		      if (this.departmentList.length < this.params.pageindex * 10){
				  this.$api.showTip('暂无更多数据');
				   return;
			  }else{
				 this.params.pageindex++;//页数加一
		         this.search();//回调接口
			  }

		    },
	}
</script>

<style lang="scss" scoped>
	.costdept{
		border-bottom: #ebebeb61 solid;
		margin-top: 20rpx;
		.costdeptChilder{
			display: flex;
			justify-content: space-between;
			padding: 20rpx 20rpx;
			font-size: 26rpx;
		}
	}

</style>
